package dao.lantern;

public class RecursoOfertaCompraDAO extends BaseDAO implements
		IRecursoOfertaCompraDAO {

	@Override
	public int countAll() throws SQLException {
		StringBuffer strbuf = new StringBuffer();

		strbuf.append("SELECT COUNT(*) FROM ");
		strbuf.append(getTableName());

		System.err.println(strbuf.toString());

		ResultSet rs = //
		connection.createStatement().executeQuery(strbuf.toString());

		rs.next();

		return rs.getInt("count");

	}

	@Override
	public void createTable() throws SQLException {
		StringBuffer strbuf;

		// ----------------------------------------

		strbuf = new StringBuffer();

		strbuf.append("DROP TABLE IF EXISTS ");
		strbuf.append(getTableName());
		strbuf.append(" CASCADE");

		System.err.println(strbuf.toString());

		connection.createStatement().execute(strbuf.toString());

		// ----------------------------------------

		strbuf = new StringBuffer();

		strbuf.append("DROP SEQUENCE IF EXISTS ");
		strbuf.append("seq_");
		strbuf.append(getTableName());

		System.err.println(strbuf.toString());

		connection.createStatement().execute(strbuf.toString());

		// ----------------------------------------

		OfertaDAO ofertaDAO = new OfertaDAO();
		OfertaDAO.init(connectionBean);

		RecursoDAO recursoDAO = new RecursoDAO();
		recursoDAO.init(connectionBean);

		strbuf = new StringBuffer();

		strbuf.append("CREATE TABLE ");
		strbuf.append(getTableName());
		strbuf.append(" (");
		strbuf.append(RecursoOfertaCompraDO.ID);
		strbuf.append(" INT PRIMARY KEY, ");
		strbuf.append(RecursoOfertaCompraDO.CANTIDAD);
		strbuf.append(" INT ,    ");
		strbuf.append(RecursoOfertaCompraDO.RECURSO_ID);
		strbuf.append(" INT REFERENCES   ");
		strbuf.append(recursoDAO.getTableName() + ", ");
		strbuf.append(RecursoOfertaCompraDO.OFERTA_ID);
		strbuf.append(" INT REFERENCES   ");
		strbuf.append(ofertaDAO.getTableName());
		strbuf.append(")");

		System.err.println(strbuf.toString());

		connection.createStatement().execute(strbuf.toString());

		// ----------------------------------------

		strbuf = new StringBuffer();

		strbuf.append("CREATE SEQUENCE ");
		strbuf.append("seq_");
		strbuf.append(getTableName());

		System.err.println(strbuf.toString());

		connection.createStatement().execute(strbuf.toString());

	}

	@Override
	public void delete(DataObject dataObject) throws SQLException {
		checkCache(dataObject, CHECK_DELETE);
		checkClass(dataObject, RecursoOfertaCompraDO.class, CHECK_DELETE);

		OfertaDO ofertaDO = (OfertaDO) dataObject;

		StringBuffer strbuf = new StringBuffer();

		strbuf.append("DELETE FROM ");
		strbuf.append(getTableName());

		strbuf.append(" WHERE ");
		strbuf.append(OfertaDO.ID);
		strbuf.append(" = ");
		strbuf.append(ofertaDO.getId());

		System.err.println(strbuf.toString());

		connection.createStatement().execute(strbuf.toString());

		dtaSession.del(dataObject);

	}

	@Override
	public void insert(DataObject dataObject) throws SQLException {
		checkCache(dataObject, CHECK_INSERT);
		checkClass(dataObject, RecursoOfertaCompraDO.class, CHECK_INSERT);

		RecursoOfertaCompraDO recursoOfertaCompraDO = (RecursoOfertaCompraDO) dataObject;

		recursoOfertaCompraDO.setId(getNextId());

		StringBuffer strbuf = new StringBuffer();

		strbuf.append("INSERT INTO ");
		strbuf.append(getTableName());
		strbuf.append(" VALUES (");
		strbuf.append(recursoOfertaCompraDO.getId());
		strbuf.append(", ");
		strbuf.append(recursoOfertaCompraDO.getCantidad());
		strbuf.append(", ");
		Reference<IRecursoDO> refR = recursoOfertaCompraDO.getRecursoRef();
		refR.checkInsert();
		strbuf.append(refR.getIdAsString());

		strbuf.append(", ");
		Reference<IOfertaDO> refO = recursoOfertaCompraDO.getOfertaRef();
		refO.checkInsert();
		strbuf.append(refO.getIdAsString());

		strbuf.append(")");

		System.err.println(strbuf.toString());

		connection.createStatement().execute(strbuf.toString());

		dtaSession.add(dataObject);

	}

	@Override
	public List<DataObject> listAll(int lim, int off) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<DataObject> listAll() throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public DataObject loadById(int id) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void update(DataObject bean) throws SQLException {
		// TODO Auto-generated method stub

	}

}
